<template>
    <div>
        <h3>商品添加</h3>
        <p>商品：<input type="text" v-model="name"></p>
        <p>价格：<input type="text" v-model="pirce"></p>
        <p>封面：<input type="file" id="img"></p>
        <p>分类：
            <select v-model="cate">
                <option v-for="w in cate_list" :value="w.id" :key="w.id">{{w.name}}</option>
            </select>
        <p><el-button type="primary" @click="get_goods">添加</el-button></p>
    </div>
</template>

<script>
import axios from 'axios'
export default {
    data() {
        return {
            name:"",
            pirce:"",
            cate:0,
            cate_list:[],
            base_url:'http://127.0.0.1:8000'           
        }
    },
    mounted(){
        this.axios({
            url: this.base_url + '/app01/cate_api',
            method:'get'
        }).then(res=>{
            this.cate_list = res.data
        }).catch(res=>{
            console.log(res.error)
        })
    },
    methods:{
        get_goods:function(){
            let img = document.getElementById('img').files[0];
            let formdata = new FormData();
            formdata.append("name",this.name);
            formdata.append("pirce",this.pirce);
            formdata.append("cate",this.cate);
            formdata.append("img",img)
            this.axios({
                url: this.base_url + '/app01/goods',
                method:"post",
                data:formdata
            }).then(res=>{
                console.log(res.data)
                alert(res.data.msg)
                }).catch(error=>{
                    console.log(error)
                })   
        }
    } 
}
</script>

<style scoped>

</style>
